package org.lzy.shop.address

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.graphics.toColorInt
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.textview.MaterialTextView
import com.google.gson.JsonArray
import com.kongzue.dialogx.dialogs.CustomDialog
import com.kongzue.dialogx.interfaces.OnBindView
import org.lzy.shop.Constant
import org.lzy.shop.R
import org.lzy.shop.adapter.ChooseAddressAdapter
import org.lzy.shop.base.BaseMvvmActivity
import org.lzy.shop.databinding.ActivityChooseaddressBinding
import org.lzy.shop.datas.DelAddress
import org.lzy.shop.response.AddressResponse
import org.lzy.shop.viewmodel.AddressModel

/**
 * 地址列表
 */
class AddressListActivity : BaseMvvmActivity<ActivityChooseaddressBinding, AddressModel>({ ActivityChooseaddressBinding.inflate(it) }) {
    // 使用by lazy确保adapter只被初始化一次且不为null
    private val chooseAddressAdapter: ChooseAddressAdapter by lazy { ChooseAddressAdapter() }

    private var mPosition: Int = 0

    override fun initView() {
        setTitleText(getResString(R.string.consignee))
        binding.recyclerview.apply {
            layoutManager = LinearLayoutManager(this@AddressListActivity, LinearLayoutManager.VERTICAL,false)
            adapter = chooseAddressAdapter
        }

        chooseAddressAdapter.addOnItemChildClickListener(R.id.item_choice_address_ll_edit) { adapter, view, position ->
            val addressResponse = adapter.getItem(position)
            addressResponse?.let {
                val bundle = Bundle()
                bundle.putSerializable("addressData", addressResponse)
                bundle.putInt("addressId", addressResponse.id)
                bundle.putInt(Constant.KEY_EXTRAS_TYPE, 2) // 2表示编辑地址
                bundle.putInt("position", position)
                startActivity(AddAddressActivity::class.java, bundle)
            }
        }

        chooseAddressAdapter.addOnItemChildClickListener(R.id.item_choice_address_ll_delete) { adapter, view, position ->
            val addressResponse = adapter.getItem(position)
            addressResponse?.let {
                mPosition = position
                showDeleteConfirmDialog(addressResponse)
            }
        }

    }

    override fun requestData() {
        getMyAddress()

    }

    override fun initData() {
        mViewModel.addressListData.observeWithLifecycle(this@AddressListActivity){addressResponses ->
            addressResponses?.let {
                if (addressResponses.isNotEmpty()) {
                    chooseAddressAdapter.submitList(addressResponses)
                } else {
                    showListEmpty()
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
    }

    fun getMyAddress(){
        mViewModel.getAddressList()
    }


    override fun allClick() {
        binding.tvNewAddAddress.setOnClickListener {
            val bundle = Bundle()
            bundle.putInt(Constant.KEY_EXTRAS_TYPE, 1)
            startActivity(AddAddressActivity::class.java, bundle)
        }
    }

    private fun showDeleteConfirmDialog(addressResponse: AddressResponse) {
        CustomDialog.show(object : OnBindView<CustomDialog?>(R.layout.dialog_common) {
            override fun onBind(dialog: CustomDialog?, v: View?) {
                val showText = v?.findViewById<TextView>(R.id.txt_msg)
                showText?.text = getResString(R.string.delete_address_hint)

                val btnOk = v?.findViewById<MaterialTextView>(R.id.btn_pos)
                val btnCancel = v?.findViewById<MaterialTextView>(R.id.btn_neg)

                btnOk?.setOnClickListener {
                    dialog?.dismiss()
                    showLoading()

                    // 创建删除请求并执行
                    val request = DelAddress().apply {
                        ids = JsonArray().apply {
                            add(addressResponse.id)
                        }
                    }

                    mViewModel.delAddress(request)
                }

                btnCancel?.setOnClickListener { dialog?.dismiss() }
            }
        }).setMaskColor("#4D000000".toColorInt())
    }

    fun showListEmpty() {
        chooseAddressAdapter.submitList(null)
        chooseAddressAdapter.isStateViewEnable = true
        chooseAddressAdapter.stateView = showEmptyView()
    }



}